package com.controller;

import com.domain.LoginUser;
import com.service.CourseService;
import com.service.LoginService;
import com.service.StudentService;
import com.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录Controller
 * 负责登入登出模块
 */
@Controller
//@RequestMapping()
public class LoginController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/login")
    public String login(String username, String password, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LoginUser user = loginService.findByName(username);
        //判断用户是否存在
        if (user == null){
            request.setAttribute("message","用户不存在!");
            return "error";
        }
        //判断密码是否正确
        if (!user.getPassword().equals(password)){
            request.setAttribute("message","密码不正确!");
            return "error";
        }
        request.getSession().setAttribute("user",user);
        if (user.getRole() == 0){
            //管理员角色
            int total = courseService.totalCourse(user.getRole(),0);
            int totalStudent = studentService.totalStudent(null);
            int totalTeacher = teacherService.totalTeacher(null);
            request.getSession().setAttribute("totalCourse",total);
            request.getSession().setAttribute("totalStudent",totalStudent);
            request.getSession().setAttribute("totalTeacher",totalTeacher);
            return "admin/menu";
        }else if (user.getRole() == 1){
            //教师角色
            int total = teacherService.findTotal(Integer.parseInt(username));
            request.getSession().setAttribute("teachTotal",total);
            return "teacher/menu";
        }else {
            //学生角色
            int total = courseService.totalCourse(user.getRole(),Integer.parseInt(user.getUserName()));    //查询总课程
            int totalAll = studentService.findAllTotal(Integer.parseInt(username)); //选修课程数
            int overC = studentService.findAllOverC(Integer.parseInt(username));    //已通过
            request.getSession().setAttribute("totalCourse",total);
            request.getSession().setAttribute("totalAll",totalAll);
            request.getSession().setAttribute("overC",overC);
            return "student/menu";
        }
    }

    /**
     * 登出系统
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().setAttribute("user",null);
        return "redirect:/login.jsp";
    }

}
